package com.zyy.smartcity1129.movie;

import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.JsonObject;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.MovieSeatInfo;
import com.zyy.smartcity1129.data.ResultInfo;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import okhttp3.RequestBody;

/**
 * @ClassName: MovieSeatFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 23/11/2021$
 * @Version: 1.0
 */

public class MovieSeatFragment extends BaseFragment {
    private int roomId = MovieTheatreFragment.movieRoomId;
    private int movieId = MovieTheatreFragment.movieMovieId;
    private int theaterId = MovieTheatreFragment.movieTheaterId;
    private int timesId = MovieTheatreFragment.movieTimesId;
    private double price = MovieTheatreFragment.moviePrice;
    private String playDate = MovieTheatreFragment.moviePlayDate;
    ExecutorService executor = Executors.newFixedThreadPool(1);
    MutableLiveData<AtomicInteger> mData = new MutableLiveData<>();
    AtomicInteger mInteger = new AtomicInteger(0);
    List<MovieSeatInfo.RowsBean> seatList = new ArrayList<>();
    private RecyclerView rvMovieSeat;
    private TextView tvMovieSeat1;
    private TextView tvMovieSeat2;
    private TextView tvMovieSeat3;
    Set<String> seats = new HashSet<String>();
    Set<Integer> seatIds = new HashSet<Integer>();

    @Override
    protected int layId() {
        return R.layout.fragment_movie_seat;
    }

    @Override
    protected String title() {
        return "选座购票";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View v) {
        initView(v);
        executor.execute(new Runnable() {
            @Override
            public void run() {
                seatList = Http.getInstance().get("/prod-api/api/movie/theatre/seat/list?roomId=" + roomId, null, MovieSeatInfo.class, 1).getRows();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
        mData.observe(this, atomicInteger -> {
            if (mInteger.get() == 1) {
                Log.i("TAG", String.valueOf(seatList.size()));
                RvAd<MovieSeatInfo.RowsBean> rvAd = new RvAd<MovieSeatInfo.RowsBean>(seatList, R.layout.rv_seat) {
                    private TextView tvRvSeat;
                    private void initView(View v) {
                        tvRvSeat = (TextView) v.findViewById(R.id.tvRvSeat);
                    }
                    @Override
                    protected void initR(View iv, MovieSeatInfo.RowsBean rowsBean, int p) {
                        initView(iv);
                        String str = rowsBean.getRow() + "排" + rowsBean.getCol() + "座";
                        tvRvSeat.setText(str);
                        tvRvSeat.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                seats.add(str);
                                seatIds.add(rowsBean.getId());
                                tvMovieSeat1.setText(seats.toString());
                            }
                        });
                    }
                };
                rvMovieSeat.setLayoutManager(new LinearLayoutManager(getContext()));
                rvMovieSeat.setAdapter(rvAd);
            }
        });
        tvMovieSeat2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                remove();
            }
        });
        tvMovieSeat3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (seatIds.size() != 0) {
                    for (int seatId : seatIds) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                JsonObject jsonObject = new JsonObject();
                                jsonObject.addProperty("movieId", movieId);
                                jsonObject.addProperty("seatId", seatId);
                                jsonObject.addProperty("roomId", roomId);
                                jsonObject.addProperty("theaterId", theaterId);
                                jsonObject.addProperty("timesId", timesId);
                                jsonObject.addProperty("price", price);
                                jsonObject.addProperty("playDate", playDate);
                                String result = Http.getInstance().get("/prod-api/api/movie/ticket", RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class, 2).getMsg();
                                Log.i("TAG", result);
                            }
                        }).start();
                    }
                    Toast.makeText(getContext(), "请支付订单", Toast.LENGTH_SHORT).show();
                    remove();
                }
            }
        });
    }

    private void initView(View v) {
        rvMovieSeat = (RecyclerView) v.findViewById(R.id.rvMovieSeat);
        tvMovieSeat1 = (TextView) v.findViewById(R.id.tvMovieSeat1);
        tvMovieSeat2 = (TextView) v.findViewById(R.id.tvMovieSeat2);
        tvMovieSeat3 = (TextView) v.findViewById(R.id.tvMovieSeat3);
    }
}
